package cst.a806yf.zyfypt_806yf.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import cst.a806yf.zyfypt_806yf.Linstener.AmicoolListener;
import cst.a806yf.zyfypt_806yf.R;
import cst.a806yf.zyfypt_806yf.Model.ResModel;


public class RegActivity extends AppCompatActivity {
     private EditText username,password,con_password,realname,email,phone;
     private RadioGroup rg;
     private RadioButton  rb_student,rb_teacher;
     private Button con_reg;

     private String roleid;
     private ResModel model;
     private AmicoolListener<String> listener;
     private Intent intent3;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);

        username=(EditText)findViewById(R.id.username);
        password=(EditText)findViewById(R.id.password);
        con_password=(EditText)findViewById(R.id.con_password);
        realname=(EditText)findViewById(R.id.realname);
        email=(EditText)findViewById(R.id.email);
        phone=(EditText)findViewById(R.id.phone);
        rg=(RadioGroup)findViewById(R.id.rg);
        rb_student=(RadioButton)findViewById(R.id.rb_student);
        rb_teacher=(RadioButton)findViewById(R.id.rb_teacher);
        con_reg=(Button)findViewById(R.id.con_reg);

        intent3=new Intent(RegActivity.this,LoginActivity.class);

        listener=new AmicoolListener<String>() {
            @Override
            public void onResponse(String l) {
                if(l.equals("1"))
                   {
                       Toast.makeText(RegActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
                       startActivity(intent3);
                       finish();
                   }
                else{
                            Toast.makeText(RegActivity.this,"注册失败",Toast.LENGTH_SHORT).show();
                     }
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(RegActivity.this,msg,Toast.LENGTH_SHORT).show();
            }
        };

          rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
              @Override
              public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                  switch(checkedId){
                       case R.id.rb_student:
                            roleid="2";
                            break;
                      case R.id.rb_teacher:
                           roleid="3";
                           break;

                  }
              }
          });

         con_reg.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                  if(con_password.getText().toString().equals(password.getText().toString()))
                     {
                            model=new ResModel("scalars");
                            model.reg(username.getText().toString(),password.getText().toString(),
                                      phone.getText().toString(),roleid,
                                      realname.getText().toString(),email.getText().toString(), listener);
                     }
                  else
                     {
                         Toast.makeText(RegActivity.this,"两次输入的密码不一致",Toast.LENGTH_SHORT).show();
                         password.setText(" ");
                         con_password.setText("");
                     }
             }
         });


    }
}
